package com.bestcem.xm.user.dao.impl;

import com.bestcem.xm.user.dao.UserRoleDao;
import com.bestcem.xm.user.dao.mapper.XmUserRoleMapper;
import com.bestcem.xm.user.entity.mysql.Role;
import com.bestcem.xm.user.entity.pojo.GetAccountRoleDO;
import com.bestcem.xm.user.entity.pojo.UserRoleDO;
import com.bestcem.xm.user.entity.pojo.UserRoleGroupDO;
import com.bestcem.xm.user.util.business.DataBaseUtil;
import com.bestcem.xm.user.util.convert.UserRoleConvert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色dao
 *
 * @author Linked <zhijian.lin@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/15 18:18
 */
@Repository
public class UseRoleDapImpl implements UserRoleDao {

    @Resource
    private XmUserRoleMapper xmUserRoleMapper;

    @Resource
    private UserRoleConvert userRoleConvert;

    @Override
    public List<UserRoleDO> selectRoleByCondition(UserRoleDO roleDO) {
        return xmUserRoleMapper.selectRoleByCondition(roleDO);
    }

    @Override
    public String saveUserRole(UserRoleDO roleDO) {
        Role role = userRoleConvert.do2Entity(roleDO);
        role.setRoleId(StringUtils.isNotBlank(role.getRoleId()) ? role.getRoleId() : DataBaseUtil.generateId());
        DataBaseUtil.setCreateAndUpdateTime(role);
        xmUserRoleMapper.insertSelective(role);
        return role.getRoleId();
    }

    @Override
    public Integer updateRoleByPrimaryKeySelective(UserRoleDO updateRole) {
        Role role = userRoleConvert.do2Entity(updateRole);
        DataBaseUtil.setUpdateTime(role);
        return xmUserRoleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public List<GetAccountRoleDO>  getAccountRoleCount(String roleId, List<String> childrenAndSelfGroupIds, Integer status) {
        return xmUserRoleMapper.getAccountRoleCount(roleId, childrenAndSelfGroupIds, status);
    }

    @Override
    public Integer deleteRoleByPrimaryKey(String roleId) {
        return xmUserRoleMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public List<UserRoleDO> getRoleListByIdList(List<String> roleIdList) {
        return xmUserRoleMapper.getRoleListByIdList(roleIdList);
    }

    @Override
    public List<UserRoleGroupDO> getRoleGroupByRoleIds(String orgId, List<String> roleIds) {
        return xmUserRoleMapper.selectRoleGroupByRoleIds(orgId, roleIds);
    }
}
